package org.tobaccomain.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private Interceptor interceptor;

    //重写addCorsMappings方法解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//允许的地址
                .allowedOriginPatterns("*")//允许的域
                .allowedMethods("*")//允许的方法
                .allowCredentials(true)//是否允许携带token
                .maxAge(3600)//设置有效期（s）这段时间内浏览器不必再次询问
                .allowedHeaders("*");
    }

    //重写addInterceptors方法自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor).addPathPatterns("/**");
    }
}
